const { Phopto } = require('./model')

module.exports = {
    async add (userId, url, albumId) {
        let _photo = await Phopto.create({
            userId,
            url,
            albumId
        })
        return _photo
    },

    async approve (id, state) {
        return Phopto.update({
            _id:id
        },{
            isApproved: state || ture
        })
    },

    async delete (id) {
        return Phopto.update({
            _id:id
        },{
            isDelete: true
        })
    },

    async getPhotos (userId, albumId, pageIndex, pageSize) {
        let result
        if (pageSize) {
            reult = await Phopto.find({
                userId,
                albumId,
                isApproved: true,
                isDelete: false
            }).sort({
                'created':-1
            }).skip((pageIndex - 1) * pageSize).limit(pageSize)
        } else {
            result = result = await Phopto.find({
                userId,
                albumId,
                isApproved: true,
                isDelete: false
            }).sort({
                'create': -1
            })
        }
        return result
    },

    async getPhotosCount (userId, albumId) {
        return Phopto.count({
            userId,
            albumId,
            isApproved: true,
            isDelete: false
        })
    },

    async getPhotosAlbumId (albumId, pageIndex, pageSize) {
        let result
        if (pageSize) {
            result = await Phopto.find({
                albumId,
                isApproved: ture,
                isDelete: false
            }).skip((pageIndex - 1) * pageSize).limit(pageSize)
        } else {
            result = await Phopto.find({
                albumId,
                isApproved: true,
                isDelete: false
            }).sort({
                'updated': -1
            })
        }
        return result
    },

    async getPhotosAlbumIdCount (albumId) {
        return Phopto.count({
            albumId,
            isApproved: true,
            isDelete: false
        })
    },

    async getApprovingPhotos (pageIndex, pageSize) {
        return Phopto.find({
            isApproved: null,
            isDelete: false
        })
    },

    async getAll (pageIndex, pageSize) {
        return Phopto.all({
            isDelete:false
        }).skip((pageIndex - 1) * pageSize).limit(pageSize)
    },

    async getApprovedPhotos (pageIndex, pageSize) {
        return Phopto.find({
            isApproved: true,
            isDelete: false
        }).skip((pageIndex - 1) * pageSize).limit(pageSize)
    },

    async getApprovedPhotosCount () {
        return Phopto.count({
            isApproved: true,
            isDelete: false
        })
    },

    async getApprovedPhotos (pageIndex, pageSize) {
        return Phopto.find({
            isApproved: false,
            isDelete: false
        }).skip((pageIndex - 1) * pageSize).limit(pageSize)
    },

    async getUnApprovedPhotosCount () {
        return Phopto.count({
            isApproved: false,
            isDelete: false
        })
    },

    async getPhotoById (id) {
        return Phopto.findById(id)
    }
   
}